/*
 * CLASS: Bookings.java
 */

package view;
import javax.swing.table.DefaultTableModel;
import model.*;

/**
 *
 * @author Samuele Mattiuzzo
 */
public class Bookings extends javax.swing.JFrame {

    Library library;
    User user;
    MainPage main;
    /** Creates new form Bookings */
    public Bookings(Library l, User u, MainPage m) {
        library = l;
        user = u;
        main = m;
        initComponents();
        if(user.isSuperuser()){
            jButtonDelete.setVisible(false);
            tableLoadAdmin();
        }else{
            jButtonConfirm.setVisible(false);
            tableLoad();
        }
    }
    /**
     * Resetta la tabella
     * @param size righe della tabella
     */
    private void tableReset(int size){
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        for(int i = 0;i < size;i++)
            tbl.removeRow(0);
    }
    /**
     * Carica la lista delle prenotazioni
     */
    private void tableLoadAdmin(){
        int size = jTable1.getRowCount();
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        for(int i = 0;i < size;i++)
            tbl.removeRow(0);
        for(Product a:library.getProducts()){
            if(!a.getBooking().isEmpty()){
                for(Booking b:a.getBooking()){
                    if(b.getBooker().canRent()){
                        String uname = b.getBooker().getUsername();
                        String title = a.getTitle();
                        int author = a.getProductId();
                        String ended = a.getRented();
                        tbl.addRow(new Object[]{uname,title,author,ended});
                    }
                }
            }
        }
    }

    /**
     * Carica la lista delle proprie prenotazioni
     */
    private void tableLoad(){
        int size = jTable1.getRowCount();
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        tableReset(size);
        for(Product a:library.getProducts()){
            if(!a.getBooking().isEmpty()){
                for(Booking b:a.getBooking()){
                    if(b.getBooker().equals(user)){
                        String uname = b.getBooker().getUsername();
                        String title = a.getTitle();
                        int author = a.getProductId();
                        String ended = a.getRented();
                        tbl.addRow(new Object[]{uname,title,author,ended});
                    }
                }
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonDetails = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jButtonConfirm = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();

        jDialog1.setTitle("");
        jDialog1.setBounds(new java.awt.Rectangle(0, 22, 300, 450));

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 960, Short.MAX_VALUE)
                .addContainerGap())
        );

        setTitle("GESTIONE PRENOTAZIONI");
        setPreferredSize(new java.awt.Dimension(680, 436));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Utente", "Titolo", "ID", "Disponibilità"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        jButtonDetails.setText("DETTAGLI");
        jButtonDetails.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDetailsMouseClicked(evt);
            }
        });

        jButtonDelete.setText("ELIMINA");
        jButtonDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteMouseClicked(evt);
            }
        });

        jButtonConfirm.setText("CONFERMA");
        jButtonConfirm.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonConfirmMouseClicked(evt);
            }
        });

        jButtonClose.setText("TERMINA MODIFICHE");
        jButtonClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCloseMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButtonDetails)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDelete)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButtonConfirm))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jButtonClose))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 269, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonDetails)
                    .add(jButtonDelete)
                    .add(jButtonConfirm))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 54, Short.MAX_VALUE)
                .add(jButtonClose)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonDetailsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDetailsMouseClicked
        if(jTable1.getSelectedRow()>-1){
            Integer i = (Integer) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 2);
            jLabel1.setText(library.getProducts().get(i).displayInfo());
            jDialog1.setVisible(true);
        }
    }//GEN-LAST:event_jButtonDetailsMouseClicked

    private void jButtonDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteMouseClicked
        if(jTable1.getSelectedRow()>-1){
            int size = jTable1.getRowCount();
            Integer i = (Integer) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 2);
            tableReset(size);
            Product p = library.getProducts().get(i);
            for(Booking b:p.getBooking()){
                if(b.getBooker().equals(user))
                  p.removeBooking(b);
            }
        }
        if(user.isSuperuser())
            tableLoadAdmin();
        else
            tableLoad();
    }//GEN-LAST:event_jButtonDeleteMouseClicked

    private void jButtonConfirmMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonConfirmMouseClicked
        if(jTable1.getSelectedRow()>-1){
            int size = jTable1.getRowCount();
            int row = jTable1.getSelectedRow();
            String username = (String) jTable1.getValueAt(row, 0);
            Integer i = (Integer) jTable1.getModel().getValueAt(row, 2);
            tableReset(size);
            Product p = library.getProducts().get(i);
            Booking book = null;
            for(Booking b:p.getBooking()){
                if(b.getBooker().getUsername().equals(username)){
                    book = b;
                    p.setRental(new Rental(b.getBooker()));
                }
                
            }
            if(book != null)
                p.removeBooking(book);
        }
    }//GEN-LAST:event_jButtonConfirmMouseClicked

    private void jButtonCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCloseMouseClicked
        this.setVisible(false);
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        tableReset(jTable1.getRowCount());
        main.setVisible(true);
    }//GEN-LAST:event_jButtonCloseMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonDetails;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
